class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}


class Solution {
    public boolean isBalanced(TreeNode root) {

      int n1=findHigh(root);

      return n1!=-1;


    }

    private int findHigh(TreeNode root) {
        if(root==null)  return 0;
        int left=findHigh(root.left);
        if(left==-1) return -1;
        int right=findHigh(root.right);
        if(right==-1) return -1;

        return Math.abs(left-right)<=1?Math.max(left,right)+1:-1;




    }
}